//
//  LIULoginViewModel.m
//  RxLogin
//
//  Created by liuyu on 16/12/16.
//  Copyright (c) 2016 limit. All rights reserved.
//

#import "LIULoginViewModel.h"

@implementation LIULoginViewModel

- (instancetype)init {
    if (self = [super init]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    _enableLoginSignal = [RACSignal combineLatest:@[RACObserve(self, account), RACObserve(self, password)] reduce:^id (NSString *account, NSString *password) {
        return @(account.length && password.length > 5);
    }];
    
    _command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            // 网络数据回调转发出去
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [subscriber sendNext:@1];
                [subscriber sendCompleted];
            });
            return nil;
        }];
    }];
    
    // 处理网络数据
    [_command.executionSignals.switchToLatest subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    [[_command.executing skip:1] subscribeNext:^(id isExec) {
        if ([isExec boolValue] == YES) {
            NSLog(@"正在执行");
        } else {
            NSLog(@"执行完成");
        }
    }];
}
@end
